package crmdna.mail2;

import static org.junit.Assert.assertEquals;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.googlecode.objectify.ObjectifyFilter;

import crmdna.client.Client;
import crmdna.common.Utils;
import crmdna.group.Group;
import crmdna.group.Group.GroupProp;
import crmdna.user.User;

public class SentMailPropTest {
  private final LocalServiceTestHelper datastoreHelper = new LocalServiceTestHelper(
      new LocalDatastoreServiceTestConfig().setApplyAllHighRepJobPolicy());

  private final String client = "isha";
  private GroupProp sgp;

  @Before
  public void setUp() {
    datastoreHelper.setUp();
    ObjectifyFilter.complete();

    Client.create(client);
    sgp = Group.create(client, "Singapore", User.SUPER_USER);
    assertEquals(1, sgp.groupId);
  }

  @After
  public void tearDown() {
    ObjectifyFilter.complete();
    datastoreHelper.tearDown();
    System.clearProperty(Mail.SYSTEM_PROPERTY_SUPPRESS_EMAIL);
  }

  @Test
  public void getActualBodyTest() {
    SentMailProp sentMailProp = new SentMailProp();
    sentMailProp.body = "Dear *|FNAME|*, This is your new password: *|PASSWORD|*";

    Map<String, String> map = new HashMap<>();
    map.put("FNAME", "Sathya");
    map.put("PASSWORD", "pass123");
    sentMailProp.mergeVars = map;

    assertEquals("Dear Sathya, This is your new password: pass123", sentMailProp.getActualBody());
  }

  @Test
  public void populateDependentsTest() {

    long mailContentId1 =
        MailContent
            .create(client, "name1", sgp.groupId, "subject1",
                "Dear *|FNAME|* *|LNAME|* we cordially invite. regards, IshaSingapore",
                User.SUPER_USER).mailContentId;

    long mailContentId2 =
        MailContent.create(client, "name2", sgp.groupId, "subject2",
            "Dear *|FNAME|* *|LNAME|* we cordially invite2. regards, IshaSingapore",
            User.SUPER_USER).mailContentId;

    Map<String, String> mergeVars = new HashMap<>();
    mergeVars.put("FNAME", "Sathya");
    mergeVars.put("LNAME", "Thilakan");
    SentMailProp prop1 = new SentMailProp();
    prop1.mailContentId = mailContentId1;
    prop1.mergeVars = mergeVars;

    SentMailProp prop2 = new SentMailProp();
    prop2.mailContentId = mailContentId2;
    prop2.mergeVars = mergeVars;

    SentMailProp.populateDependents(client, Utils.getList(prop1, prop2));

    assertEquals("subject1", prop1.subject);
    assertEquals("Dear Sathya Thilakan we cordially invite. regards, IshaSingapore", prop1.body);

    assertEquals("subject2", prop2.subject);
    assertEquals("Dear Sathya Thilakan we cordially invite2. regards, IshaSingapore", prop2.body);

  }
}
